%% it produces Figure O3


% load calibration south
 str_region="South_";
str1="params_";
 str_3="calibration_Table2";
str_save=append(path_calibration,str1,str_region,str_3,".mat");
 filename=cell2mat(str_save);
 load(filename)
 read_pars;read_pars_s;

%% obtain size and timing of ISS
get_ISS_subsidy;

%% obtain figures for policy at south calibration
calib=1;a= (a_targ-(1-phi)*varphi)/phi;
get_firm_policy;
get_figures_policy;
b_up_ss=b_up;  b_md_ss=b_md;  b_dn_ss=b_dn;

%% obtain policy at different values of value added
A_change = 0.5:0.05:1.05;
Amin = A_change(1)*A_s;
calib=0;v_vec=[];x_vec=[];inve_vec=[];drift_vec=[];bbar_vec=[];ell_vec=[];btsar_vec=[];
for ia=1:length(A_change)
    A=A_s*A_change(ia);
    a=(a_targ-(1-phi)*varphi)/phi;
    get_firm_policy;
    v_vec(:,ia)=v;
    x_vec(:,ia)=x;
    inve_vec(:,ia)=inve;
    drift_vec(:,ia)=drift_I;
    bbar_vec(ia) = b_bar;
    ell_vec(:,ia)= ell;
    btsar_vec(ia)= b_vec_d(inde_b_star);
end

A_vec     = A_change*A_s;
cub_spl_v = csapi({b_vec_d',A_vec},v_vec);
cub_spl_x = csapi({b_vec_d',A_vec},x_vec);
cub_spl_i = csapi({b_vec_d',A_vec},inve_vec);
cub_spl_d = csapi({b_vec_d',A_vec},drift_vec);
cub_spl_l = csapi({b_vec_d',A_vec},ell_vec);
pp_bar    = spline(A_vec,bbar_vec);
pp_star   = spline(A_vec,btsar_vec);

% figures to evaluate policy
A_1 = A_s*0.66;
va_to_prof_0 = A_s/(A_s/nu-chi);
mu_0=(A_s/nu-chi);
va_to_prof_1 = A_1/(A_1/nu-chi);
mu_1=(A_1/nu-chi);
ba_0         = A_s*b_vec_d/(A_s/nu-chi);
ba_1         = A_1*b_vec_d/(A_1/nu-chi);

mu_1/mu_0

for ii=1:length(b_vec_d)
    b_i = b_vec_d(ii);
    bbar_0    = ppval(pp_bar,A_s);
    bbar_1    = ppval(pp_bar,A_1);
    ell_0(ii) = fnval( cub_spl_l, [b_i; A_s] );
    ell_1(ii) = fnval( cub_spl_l, [b_i; A_1] );
    inv_0(ii) = fnval( cub_spl_d, [b_i; A_s] );
    inv_1(ii) = fnval( cub_spl_d, [b_i; A_1] );
    v_0(ii) = fnval( cub_spl_v, [b_i; A_s] );
    v_1(ii) = fnval( cub_spl_v, [b_i; A_1] );
    x_0(ii) = fnval( cub_spl_x, [b_i; A_s] );
    x_1(ii) = fnval( cub_spl_x, [b_i; A_1] );
end

inde_tresh=find(b_vec_d>bbar_0,1)-1;
inde_tresh2=find(b_vec_d>bbar_1,1)-1;

figure(400)
plot(ba_1(b_vec_d<bbar_1),va_to_prof_1*ell_1(b_vec_d<bbar_1),'--r','LineWidth',6),hold on
plot(ba_0(b_vec_d<bbar_0),va_to_prof_0*ell_0(b_vec_d<bbar_0),'b','LineWidth',6),hold off
xline(ba(inde_tresh),'--b','Bankruptcy','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','right')
xline(ba(inde_tresh2),'--r','Bankruptcy: low $\mu$','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','left')
grid on
le=legend('Low $\mu$','Baseline');
set(le,'Interpreter','Latex','Fontsize',16,'Location','NorthWest');
xlabel('Debt ratio $b$','Fontsize',24,'Interpreter','Latex')
ylabel('Debt issuance: $\ell$','Fontsize',24,'Interpreter','Latex')
settt,xlim([0 27])
grid on
str1="/figures/figure_ell_mu.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)

figure(401)
plot(ba_1(b_vec_d<bbar_1-0.1),100*inv_1(b_vec_d<bbar_1-0.1),'--r','LineWidth',6),hold on
plot(ba_0(b_vec_d<bbar_0),100*inv_0(b_vec_d<bbar_0),'b','LineWidth',6),hold off
xline(ba(inde_tresh),'--b','Bankruptcy','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','right')
xline(ba(inde_tresh2),'--r','Bankruptcy: low $\mu$','Interpreter','Latex','Fontsize',18,'LabelHorizontalAlignment','left')
grid on
le=legend('Low $\mu$','Baseline');
set(le,'Interpreter','Latex','Fontsize',16,'Location','southwest');
xlabel('Debt ratio $b$','Fontsize',24,'Interpreter','Latex')
ylabel('Investment rate: $\iota$, in \%','Fontsize',24,'Interpreter','Latex')
settt,xlim([0 27])
grid on
str1="/figures/figure_iota_mu.pdf";
str_save1=append(root,str1);
print(gcf,'-dpdf',str_save1)


%% Evaluate Ratchet effect at entry
get_figure_rachet_effect;

